import 'package:extended_image/extended_image.dart';
import 'package:flutter/material.dart';

class NetImage extends ExtendedImage {
  NetImage._({
    required super.image,
    super.key,
    super.semanticLabel,
    super.excludeFromSemantics,
    super.width,
    super.height,
    super.color,
    super.opacity,
    super.colorBlendMode,
    super.fit,
    super.alignment,
    super.repeat,
    super.centerSlice,
    super.matchTextDirection,
    super.gaplessPlayback,
    super.filterQuality,
    super.loadStateChanged,
    super.border,
    super.shape,
    super.borderRadius,
    super.clipBehavior,
    super.enableLoadState,
    super.beforePaintImage,
    super.afterPaintImage,
    super.mode,
    super.clearMemoryCacheIfFailed,
    super.onDoubleTap,
    super.initGestureConfigHandler,
    super.enableSlideOutPage,
    super.constraints,
    super.extendedImageEditorKey,
    super.initEditorConfigHandler,
    super.heroBuilderForSlidingPage,
    super.clearMemoryCacheWhenDispose,
    super.extendedImageGestureKey,
    super.isAntiAlias,
    super.handleLoadingProgress,
    super.layoutInsets,
  });

  factory NetImage(
    String url, {
    Key? key,
    String? semanticLabel,
    bool excludeFromSemantics = false,
    double? width,
    double? height,
    Color? color,
    Animation<double>? opacity,
    BlendMode? colorBlendMode,
    BoxFit? fit = BoxFit.cover,
    AlignmentGeometry alignment = Alignment.center,
    ImageRepeat repeat = ImageRepeat.noRepeat,
    Rect? centerSlice,
    bool matchTextDirection = false,
    bool gaplessPlayback = false,
    FilterQuality filterQuality = FilterQuality.low,
    LoadStateChanged? loadStateChanged,
    BoxBorder? border,
    BoxShape? shape,
    BorderRadius? borderRadius,
    Clip clipBehavior = Clip.antiAlias,
    bool enableLoadState = true,
    BeforePaintImage? beforePaintImage,
    AfterPaintImage? afterPaintImage,
    ExtendedImageMode mode = ExtendedImageMode.none,
    bool clearMemoryCacheIfFailed = true,
    DoubleTap? onDoubleTap,
    InitGestureConfigHandler? initGestureConfigHandler,
    bool enableSlideOutPage = false,
    BoxConstraints? constraints,
    Key? extendedImageEditorKey,
    InitEditorConfigHandler? initEditorConfigHandler,
    HeroBuilderForSlidingPage? heroBuilderForSlidingPage,
    bool clearMemoryCacheWhenDispose = false,
    Key? extendedImageGestureKey,
    bool isAntiAlias = false,
    bool handleLoadingProgress = false,
    EdgeInsets layoutInsets = EdgeInsets.zero,
    Map<String, String>? headers,
    int retries = 3,
    Duration? timeLimit,
    bool cache = true,
    double scale = 1.0,
    Duration timeRetry = const Duration(milliseconds: 100),
    String? cacheKey,
    bool printError = true,
    double? compressionRatio,
    int? maxBytes,
    bool cacheRawData = false,
    String? imageCacheName,
    Duration? cacheMaxAge,
    WebHtmlElementStrategy webHtmlElementStrategy = WebHtmlElementStrategy.never,
  }) {
    return NetImage._(
      image: ExtendedNetworkImageProvider(
        url,
        scale: scale,
        headers: headers,
        cache: cache,
        retries: retries,
        timeRetry: timeRetry,
        timeLimit: timeLimit,
        cacheKey: cacheKey,
        printError: printError,
        cacheRawData: cacheRawData,
        imageCacheName: imageCacheName,
        cacheMaxAge: cacheMaxAge,
        webHtmlElementStrategy: webHtmlElementStrategy,
      ),
      key: key,
      semanticLabel: semanticLabel,
      excludeFromSemantics: excludeFromSemantics,
      width: width,
      height: height,
      color: color,
      opacity: opacity,
      colorBlendMode: colorBlendMode,
      fit: fit,
      alignment: alignment,
      repeat: repeat,
      centerSlice: centerSlice,
      matchTextDirection: matchTextDirection,
      gaplessPlayback: gaplessPlayback,
      filterQuality: filterQuality,
      loadStateChanged: loadStateChanged ?? _defaultLoadStateChange,
      border: border,
      shape: shape,
      borderRadius: borderRadius,
      clipBehavior: clipBehavior,
      enableLoadState: enableLoadState,
      beforePaintImage: beforePaintImage,
      afterPaintImage: afterPaintImage,
      mode: mode,
      clearMemoryCacheIfFailed: clearMemoryCacheIfFailed,
      onDoubleTap: onDoubleTap,
      initGestureConfigHandler: initGestureConfigHandler,
      enableSlideOutPage: enableSlideOutPage,
      constraints: constraints,
      extendedImageEditorKey: extendedImageEditorKey,
      initEditorConfigHandler: initEditorConfigHandler,
      heroBuilderForSlidingPage: heroBuilderForSlidingPage,
      clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
      extendedImageGestureKey: extendedImageGestureKey,
      isAntiAlias: isAntiAlias,
      handleLoadingProgress: handleLoadingProgress,
      layoutInsets: layoutInsets,
    );
  }

  static Widget? _defaultLoadStateChange(ExtendedImageState state){
    switch (state.extendedImageLoadState) {
      case LoadState.loading:
        return Center(
          child: CircularProgressIndicator(),
        );
      case LoadState.completed:
        return null;
      case LoadState.failed:
        return Center(
          child: Icon(Icons.error),
        );
    }
  }
}